/*
 * TCSS 445 - Major Project
 * 
 * Author: Chris Davidson
 * Author: Dallas Rosson
 */
package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import model.Book;

/**
 * BookDataInputFrame
 * 
 * The frame used to get the data for a new book from the user.
 *
 * @author Dallas Rosson
 * @author Chris Davidson
 * @version 1.0, 28 May 2012
 */
public class BookDataInputFrame {
    
    /**
     * The frame.
     */
    private final JFrame my_frame = new JFrame();
    
    /**
     * The patron.
     */
    private final Book my_book;
    
    /**
     * The name text input field.
     */
    private JTextField my_ISBNTxt = new JTextField();
        
    /**
     * The address text input field.
     */
    private JTextField my_titleTxt = new JTextField();
        
    /**
     * The state text input field.
     */
    private JTextField my_authorTxt = new JTextField();
        
    /**
     * The zip text input field.
     */
    private JTextField my_yearTxt = new JTextField();
        
        
    /**
     * Constructs a new BookDataInputFrame.
     * 
     * @param the_book The patron to store the data in.
     */
    public BookDataInputFrame(final Book the_book) {
        
        my_book = the_book;
    }
    
    /**
     * Initializes and instantiates the frame.
     */
    public void initialize() {
        
        my_frame.setPreferredSize(new Dimension(350, 250));
        my_frame.setResizable(false);
        my_frame.setVisible(true);
        my_frame.setTitle("Newport Oldtown Library Catalog - New Book Input");
        my_frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        
        JPanel mainPanel = new JPanel();
        
        JLabel ISBNLbl = new JLabel("ISBN:     ");
        ISBNLbl.setPreferredSize(new Dimension(75, 25));
        
        JLabel titleLbl = new JLabel("Title:    ");
        titleLbl.setPreferredSize(new Dimension(75, 25));
        
        JLabel authorLbl = new JLabel("Author:   ");
        authorLbl.setPreferredSize(new Dimension(75, 25));
        
        JLabel yearLbl = new JLabel("Year:     ");
        yearLbl.setPreferredSize(new Dimension(75, 25));
        
        my_ISBNTxt.setPreferredSize(new Dimension(220, 25));
        my_ISBNTxt.setBorder(BorderFactory.createLineBorder(Color.black));
        
        my_titleTxt.setPreferredSize(new Dimension(220, 25));
        my_titleTxt.setBorder(BorderFactory.createLineBorder(Color.black));
        
        my_authorTxt.setPreferredSize(new Dimension(220, 25));
        my_authorTxt.setBorder(BorderFactory.createLineBorder(Color.black));
        
        my_yearTxt.setPreferredSize(new Dimension(220, 25));
        my_yearTxt.setBorder(BorderFactory.createLineBorder(Color.black));
        
        mainPanel.setLayout(new GridLayout(5, 1));
        
        JPanel ISBNPnl = new JPanel();
        ISBNPnl.setLayout(new FlowLayout());
        
        JPanel titlePnl= new JPanel();
        titlePnl.setLayout(new FlowLayout());
        
        JPanel authorPnl = new JPanel();
        authorPnl.setLayout(new FlowLayout());
        
        JPanel yearPnl = new JPanel();
        yearPnl.setLayout(new FlowLayout());
        
        ISBNPnl.add(ISBNLbl);
        ISBNPnl.add(my_ISBNTxt);
        titlePnl.add(titleLbl);
        titlePnl.add(my_titleTxt);
        authorPnl.add(authorLbl);
        authorPnl.add(my_authorTxt);
        yearPnl.add(yearLbl);
        yearPnl.add(my_yearTxt);
        
        mainPanel.add(ISBNPnl);
        mainPanel.add(titlePnl);
        mainPanel.add(authorPnl);
        mainPanel.add(yearPnl);
        
        JButton createBookBtn = new JButton("Create Book");
        
        createBookBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent the_e) {
                
                my_book.setISBN(Integer.parseInt(my_ISBNTxt.getText()));
                my_book.setTitle(my_titleTxt.getText());
                my_book.setAuthor(my_authorTxt.getText());
                my_book.setYear(Integer.parseInt(my_ISBNTxt.getText()));
                
                my_frame.dispose();
            }
        });
        
        JButton cancelBtn = new JButton("Cancel");
        
        cancelBtn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent the_e) {
                
                my_frame.dispose();
            }
        });
        
        JPanel buttonPnl = new JPanel();
        buttonPnl.add(createBookBtn);
        buttonPnl.add(cancelBtn);
        
        mainPanel.add(buttonPnl);
        
        my_frame.add(mainPanel);
        
        
        my_frame.pack();
    }
    
}
